// ---------------路由初始化-----------------
import Vue from 'vue'
import VueRouter from 'vue-router'

// -------------------组件导入---------------
import Login from '../pages/login.vue'
import Register from '../pages/register.vue'
import Home from '../pages/home.vue'
import Forget from '../pages/forgetPassword.vue'
import User from '../pages/user.vue'
import Ceshi from '../pages/ceshi.vue'
import customerIfo from '../pages/customerIfo.vue'
import customerDetails from '../pages/customerDetails.vue'
import OutLine from '../pages/outLine.vue'

// -----------------加载message-------------
import { Message } from 'element-ui'
Vue.prototype.$message = Message

Vue.use(VueRouter)
const router = new VueRouter({
  routes: [
    { path: '/', redirect: '/home' },
    {
      path: '/home',
      component: Home,
      name: 'home',
      children: [
        { path: '/', component: User, name: 'user' },
        { path: '/home/customer-ifo', component: customerIfo, name: 'customer-ifo' },
        { path: '/home/customer-details', component: customerDetails, name: 'customer-details' },
        { path: '/home/out-line', component: OutLine, name: 'out-line' },

      ]
    },
    { path: '/login', component: Login, name: 'login' },
    { path: '/register', component: Register, name: 'register' },
    { path: '/forget', component: Forget, name: 'forget' },
    { path: '/user', component: User, name: 'user' },
    { path: '/ceshi', component: Ceshi, name: 'ceshi' },


  ],

})
// 解决导航报错问题
import Router from 'vue-router'
const originalPush = Router.prototype.push
Router.prototype.push = function push(location, onResolve, onReject) {
  if (onResolve || onReject) return originalPush.call(this, location, onResolve, onReject)
  return originalPush.call(this, location).catch(err => err)
}

//路由导航守卫
router.beforeEach((to, from, next) => {

  if (to.path === '/login') return next();
  //判断其他页面是否有token
  const token = localStorage.getItem('token');
  //存在继续往后走
  if (token) return next();

  Message({
    showClose: true,
    message: '未登录，请重新登陆！',
    type: 'warning',
    offset: 20
  })
  router.push({
    name: 'login'
  })

});
export default router